<?php

namespace App\Package\Finance;

use App\Models\Finance\Template;
use App\Models\Finance\Regulation;
use Illuminate\Support\Facades\Log;

/**
 * 自定义字段处理
 *
 * Date: 2018/6/13
 * @author George
 * @package App\Package\Finance
 */
class FieldHandler
{
	/**
	 * 定义存储字段信息的属性
	 *
	 * @var array
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $field;

	/**
	 * 定义保存字段计算规则的属性
	 *
	 * @var Regulation $regulation
	 * Date: 2018/6/13
	 * @author George
	 */
	protected $regulation;

	/**
	 * FieldHandler constructor.
	 * @param $field
	 */
	public function __construct(array $field)
	{
		$this->field = $field;
	}

	/**
	 * 处理逻辑
	 *
	 * Date: 2018/6/13
	 * @author George
	 * @return array
	 */
	public function handle()
	{
		if (is_array($this->field)) {
			// 获取字段的计算规则
			$this->regulation = Regulation::query()->where('field_id', $this->field['id'])->first();
			switch ($this->regulation->type) {
				case 'value':
					// 当字段规则为固定值时直接赋值
					$this->field['value'] = $this->regulation->value;
					break;
				case 'sample':
					// 当字段规则为简单公式时进行运算
					$expression = new Expression($this->regulation->literals);
					$field['value'] = $expression->result();
					break;
				case 'logic':
					// 当字段规则为逻辑公式是，进行逻辑运算
					$template = Template::with('conditions')->where('id', $this->regulation->template_id)->firstOrFail();
					// 判断模板类型根据不同类型进行计算获取最终值
					switch ($template->type) {
						case 'float':
						case 'staircase':
						case 'advanced':
					}
			}
		} else {
			Log::error('薪资字段有误');
		}

		return $this->field;
	}
}
